package com.tz.farmer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tz.farmer.model.FarmerPermissionInfo;
import com.tz.farmer.model.FarmerRoleInfo;
import com.tz.farmer.so.FarmerRoleInfoSo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SmartAdminRoleInfoMapper
 * @Description TODO
 * @Creator kaikai
 * @Date 2021/5/11
 */
public interface FarmerRoleInfoMapper extends BaseMapper<FarmerRoleInfo> {
    @Select("SELECT * FROM farmer_permission_info ORDER BY FUN_INDEX ASC, FUN_SEQ ASC  ")
    List<FarmerPermissionInfo> getTreeData();

    @Select("select fun_id from role_permission_info where role_id=#{roleId}  ")
    List<String> queryFunByRole(@Param("roleId")Integer roleId);


    @Select("<script> select a.*,b.fun_id from farmer_role_info a left join (select role_id,GROUP_CONCAT(fun_id) as fun_id from role_permission_info group by role_id) b on a.id=b.role_id "+
            " where a.is_delete='0' " +
            " <if test = \"roleName != null and roleName.trim() !=''  \"  > " +
            " and a.role_name = #{roleName} " +
            " </if> "+
            " order by create_time desc "+
            " limit #{n} , #{m}" +
            "</script>"
    )

    List<FarmerRoleInfoSo> selectRoles(@Param("roleName")String roleName , @Param("n")int n, @Param("m")int m);

    @Select("<script> select count(1) from  farmer_role_info"+
            " where is_delete='0' " +
            " <if test = \"roleName != null and roleName.trim() !=''  \"  > " +
            " and role_name = #{roleName} " +
            " </if> "+
            "</script>"
    )

    int countTotal(@Param("roleName")String roleName );

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert  into farmer_role_info (role_name,role_text,create_time) values (#{roleName},#{roleText},#{createTime})")
    int insert(FarmerRoleInfo farmerRoleInfo);
}
